<template>
  <div class="setting-company">
    <el-card class="tree-card">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="角色管理" name="first">
          <el-button
            size="mini"
            type="primary"
            @click="addRoleHandle"
          >添加</el-button>
          <el-table
            border
            :data="tableData"
            style="width: 100%"
            :default-sort="{ prop: 'date', order: 'descending' }"
          >
            <el-table-column type="index" width="50" label="序号" />
            <el-table-column prop="name" label="角色名" sortable width="180" />
            <el-table-column sortable prop="description" label="描述" />

            <el-table-column label="操作">
              <template slot-scope="{ row }">
                <!-- <pre>{{ row }}</pre> -->
                <el-button
                  size="mini"
                  @click="setRole(row)"
                >分配权限</el-button>
                <el-button size="mini">修改</el-button>
                <el-button size="mini" type="danger">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-form
            class="formInfo"
            disabled
            label-width="80px"
            :model="formLabelAlign"
          >
            <el-form-item label="企业名称">
              <el-input v-model="formLabelAlign.name" />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input v-model="formLabelAlign.companyAddress" />
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input v-model="formLabelAlign.companyPhone" />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input v-model="formLabelAlign.mailbox" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="formLabelAlign.remarks" />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

      <!-- <el-pagination
        :current-page.sync="currentPage1"
        :page-size="100"
        layout="total, prev, pager, next"
        :total="1000"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->

      <div style="display:flex;justify-content:flex-end;align-items:center">
        共{{ total }}条
        <el-pagination
          :total="total"
          :page-size="page.pagesize"
          layout="prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <!-- 添加对话框 -->
      <el-dialog title="添加角色" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="角色名称" label-width="120px">
            <el-input v-model.trim="form.name" autocomplete="off" />
          </el-form-item>
          <el-form-item label="角色描述" label-width="120px">
            <el-input v-model.trim="form.description" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="btnAdd">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 分配权限 -->
      <el-dialog
        :title="'为【' + title + '】分配权限'"
        :visible.sync="dialogFormVisibleSetRole"
      >
        <el-tree
          ref="tree"
          :data="data"
          show-checkbox
          node-key="id"
          :default-checked-keys="defaultKeys"
          default-expand-all
          check-strictly
          :props="defaultProps"
        />
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisibleSetRole = false">取 消</el-button>
          <el-button type="primary" @click="setPermission">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import {
  getCompanyInfo,
  getRoleList,
  addRole,
  getRoleDetail,
  assignPerm
} from '@/api/setting'
import { getPermissionList } from '@/api/permission'
import { mapState } from 'vuex'
import { tranListToTreeData } from '@/utils'
export default {
  data() {
    return {
      currentPage1: 1,
      activeName: 'first',
      formLabelAlign: {},
      total: 0,
      tableData: [],
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      },
      page: {
        page: 1,
        pagesize: 3
      },

      /* 分配权限 */
      dialogFormVisibleSetRole: false,
      title: '111',

      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permIds: [],
      id: '',
      defaultKeys: [] //  当前端的权限点
    }
  },
  created() {
    this.getInfo()
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    formatter(row, column) {
      return row.address
    },

    async getInfo() {
      const { total, rows } = await getRoleList({
        id: this.userInfo.companyId,
        ...this.page
      })
      this.total = total
      this.tableData = rows

      this.formLabelAlign = await getCompanyInfo(this.userInfo.companyId)
    },

    /* 分页 */
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.page.page = val
      this.getInfo()
    },

    /* 添加角色 */
    addRoleHandle() {
      this.dialogFormVisible = true
    },
    /* 点击对话框确认按钮 */
    async btnAdd() {
      await addRole(this.form)
      this.getInfo()
      this.form = {
        name: '',
        region: ''
      }
      this.dialogFormVisible = false
    },

    /* 分配权限 */
    async setRole(row) {
      this.defaultKeys = []
      this.id = row.id

      this.title = row.name

      /* 获取所有的权限 */
      this.data = tranListToTreeData(await getPermissionList(), 'pid', 'id')

      /* 根据id获取角色权限 */
      const res = await getRoleDetail(row.id)
      this.defaultKeys = res.permIds
      this.dialogFormVisibleSetRole = true
    },

    /* 点击对话框按钮 分配权限 */
    async setPermission() {
      // console.log(this.$refs.tree.getCheckedKeys(), 999)
      this.permIds = this.$refs.tree.getCheckedKeys()
      await assignPerm({
        id: this.id,
        permIds: this.permIds
      })
      this.dialogFormVisibleSetRole = false
    }
  }
}
</script>
<style lang="scss" scoped>
.setting-company {
  .el-form.formInfo {
    width: 400px;
    margin-left: 100px;
  }
}
</style>
